package com.cuijiao.emp.java.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.cuijiao.emp.java.model.Emp;
import com.cuijiao.emp.java.util.BaseDao;
import com.cuijiao.emp.java.util.DateUtil;
import com.cuijiao.emp.java.util.JDBCUtil;

/**
 * 实现接口的所有方法，并继承了父类的所有方法
 *
 * @author cuijiao
 *
 */
public class EmpDaoSQL extends BaseDao implements EmpDao {
    /**
     * 废掉，用不到了
     */
    @Override
    public int getIndexByEmpNo(String empNo) {
        return -1;
    }

    @Override
    public Emp getEmpByEmpNo(String empNo) {
        Connection conn = JDBCUtil.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from emp where emp_no=?";
        Emp emp = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, empNo);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                boolean sex = rs.getBoolean("sex");
                String empName = rs.getString("emp_name");
                String birthday = rs.getString("birthday");
                double salary = rs.getDouble("salary");
                emp = new Emp(empName, empNo, salary, sex, DateUtil.string2Date(birthday));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            JDBCUtil.free(conn, pstmt, rs);
        }

        return emp;
    }

    @Override
    public int deleteEmpByEmpNo(String empNo) {
        String sql = "delete from emp where emp_no=?";
        return this.updateByPrepared(sql, empNo);

    }

    @Override
    public int modifyEmpByEmpNo(Emp emp) {

        String sql = "update emp set emp_name=?,sex=?,salary=?,birthday=? where emp_no=?";
        return this.updateByPrepared(sql, emp.getEmpName(), emp.isSex(), emp.getSalary(), emp.getBirthday(), emp.getEmpNo());

    }

    @Override
    public int addEmp(Emp emp) {
        String sql = "insert into emp values(?,?,?,?,?)";
        return this.updateByPrepared(sql, emp.getEmpNo(), emp.getEmpName(), emp.isSex(), emp.getSalary(), DateUtil.date2Sring(emp.getBirthday()));

    }

    @Override
    public List<Emp> getEmps() {
        Connection conn = JDBCUtil.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select * from emp";
        List<Emp> empList = new ArrayList<Emp>();
        Emp emp = null;
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                String empNo = rs.getString("emp_no");
                boolean sex = rs.getBoolean("sex");
                String empName = rs.getString("emp_name");
                String birthday = rs.getString("birthday");
                double salary = rs.getDouble("salary");
                emp = new Emp(empName, empNo, salary, sex, DateUtil.string2Date(birthday));
                empList.add(emp);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            JDBCUtil.free(conn, pstmt, rs);
        }
        return empList;
    }

}
